//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

#include "zmtp.h"
#include "zmtp_dealer.h"
#include "ZMQ_Switcher.h"

/**
 * 这两个数据结构定义(_zmtp_channel_t, _zmtp_dealer_t)来源于其它文件
 * 复制过来，必须与来源保持一致
 */
// from: zmtp_channel.h
struct _zmtp_channel_t {
    int fd;             // BSD socket handle
    int zmtpDealerId;   // fd come from caller, needn't close it
};
typedef struct _zmtp_channel_t zmtp_channel_t;

// from zmtp_dealer.c
struct _zmtp_dealer_t {
    zmtp_channel_t *channel;
};

static struct _zmtp_dealer_t *g_dealers[MAX_ZMTP_DEALER];

/**
 * zmtp_voter_init
 */
int zmtp_voter_env_init()
{
    int i;

#ifdef Use_CMemPoolSet
    if (zmtp_CMemPoolSet() == NULL) {
        return -1;
    }
#endif

    for (i = 0;  i < MAX_ZMTP_DEALER;  i++) {
        g_dealers[i] = zmtp_dealer_new();
        if (NULL == g_dealers[i]) {
            return i;
        }
    }

    return i;
}

/**
 * zmtp_voter_destroy
 */
int zmtp_voter_env_destroy()
{
    int i;

    for (i = 0;  i < MAX_ZMTP_DEALER;  i++) {
        zmtp_dealer_destroy(&(g_dealers[i]));
    }

    return i;
}

/**
 * zmtp_voter_listen
 *
 * 成功: 返回值0
 */
int zmtp_voter_listen(int idxDealer, int fd)
{
    if (idxDealer < 0) {
        for (int i = 0;  i < MAX_ZMTP_DEALER;  i++) {
            if (NULL == g_dealers[i]->channel) {
                idxDealer = i;
                break;
            }
        }
    }

    zmtp_channel_t channel = {fd, idxDealer};

    // g_dealers[idxDealer]->channel 将被 zmtp_dealer_listen 赋值
    int iRet = zmtp_dealer_listen(g_dealers[idxDealer], NULL, (void*)&channel);

    return iRet;
}

/**
 * zmtp_voter_destroy
 *
 * 成功: 返回值0
 */
void zmtp_voter_destroy(int idxDealer)
{
    if ((idxDealer < 0) || (idxDealer >= MAX_ZMTP_DEALER)) {
        return;
    }

    zmtp_dealer_destroy(&(g_dealers[idxDealer]));
}

/**
 * zmtp_voter_recv
 */
char *zmtp_voter_recv(int idxDealer, char *buf, size_t size)
{
    if ((idxDealer < 0) || (idxDealer >= MAX_ZMTP_DEALER) || (NULL == buf)) {
        return NULL;
    }

    zmtp_msg_t *msg = zmtp_dealer_recv(g_dealers[idxDealer]);
    if (NULL == msg) {
        return NULL;
    }

    byte *bytes = zmtp_msg_data(msg);

    if (zmtp_msg_size(msg) < size) {
        memcpy(buf, bytes, zmtp_msg_size(msg));
        zmtp_msg_destroy(&msg);
        return (char*)bytes;
    }

    zmtp_msg_destroy(&msg);
    return NULL;
}

/**
 * zmtp_voter_send
 */
int zmtp_voter_send(int idxDealer, char *buf, size_t size)
{
    if ((idxDealer < 0) || (idxDealer >= MAX_ZMTP_DEALER) || (NULL == buf)) {
        return -1;
    }

    zmtp_msg_t *msg = zmtp_msg_from_data(ZMTP_MSG_COMMAND, (byte **)&buf, size);
    if (NULL == msg) {
        return -2;
    }

    int iRet = zmtp_dealer_send(g_dealers[idxDealer], msg);

    zmtp_msg_destroy(&msg);
    return iRet;
}
